package com.zzy.mccjt.service.common.util;

import com.alibaba.fastjson.JSONObject;
import com.zzy.mccjt.pojo.common.AdminConstants;
import com.zzy.mccjt.pojo.common.CurrentUser;
import com.zzy.mccjt.service.common.framework.constants.ContextUserLocal;
import com.zzy.mccjt.service.common.framework.constants.HeaderConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * description:
 *
 * @author zzy
 * @version 1.0
 * @date 2020/4/3
 */
public class UserUtils {

    /**
     * 获取当前reques
     *
     * @return
     */
    public static HttpServletRequest request() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        if (servletRequestAttributes != null) {
            return servletRequestAttributes.getRequest();
        }
        return null;
    }

    /**
     * 获取token
     *
     * @return
     */
    public static String getToken() {
        try {
            HttpServletRequest request = request();
            if (request != null) {
                return request.getHeader(HeaderConstants.REQUEST_HEADER_AUTH);
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

        // 取不到从ContextUserLocal取
        String token = Optional.ofNullable(ContextUserLocal.get())
                .map(ContextUserLocal::getToken)
                .orElse("");
        return token;
    }

    public static CurrentUser currentUser() {
        CurrentUser user = new CurrentUser(AdminConstants.USER_ID, AdminConstants.USER_LOGIN_NAME);
        String token = getToken();
        if (token != null && !token.isEmpty()) {
            // 解析token
            String subject = JwtUtil.getSubject(token);
            if (org.apache.commons.lang3.StringUtils.isNotBlank(subject)) {
                user = JSONObject.parseObject(subject, CurrentUser.class);
            }
        }
        return user;
    }

    /**
     * 获取当前登陆的商户id
     *
     * @return
     */
    public static Integer currentMerchantId() {
        try {
            HttpServletRequest request = request();
            if (request != null) {
                String str = request.getHeader(HeaderConstants.MULTI_TENANT_ID);
                if (StringUtils.isNotBlank(str)) {
                    return Integer.parseInt(str);
                }
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

        // 取不到从ContextUserLocal取
        Integer merchantId = Optional.ofNullable(ContextUserLocal.get())
                .map(ContextUserLocal::getTenantId)
                .orElse(null);
        return merchantId;
    }

    /**
     * 获取当前用户id
     *
     * @return
     */
    public static Long currentUserId() {
        Long userId = null;
        try {
            CurrentUser user = currentUser();
            if (user != null) {
                userId = user.getUserId();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userId;
    }

    /**
     * 获取当前用户id并转换为string
     *
     * @return
     */
    public static String currentUserIdStr() {
        Long userId = currentUserId();
        if (userId != null) {
            return String.valueOf(userId);
        }
        return null;
    }

    /**
     * 获取当前用户账号
     *
     * @return
     */
    public static String currentUserAccount() {
        String account = null;
        try {
            CurrentUser user = currentUser();
            if (user != null) {
                account = user.getName();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return account;
    }

    /**
     * 获取当前用户姓名
     *
     * @return
     */
    public static String currentUserName() {
        String userName = null;
        try {
            CurrentUser user = currentUser();
            if (user != null) {
                userName = user.getName();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userName;
    }

    /**
     * 获取请求头的Platform
     * @return
     */
    public static String getRequestPlatform() {
        String str = null;
        try {
            HttpServletRequest request = request();
            if (request != null) {
                str = request.getHeader(HeaderConstants.REQUEST_HEADER_PLATFORM);
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        return str;
    }

}
